package com.baizhi.serviceimpl;

import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import com.baizhi.entity.UserExample;
import com.baizhi.service.AdminService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Transactional
@Service
public class AdminServiceImpl implements AdminService {

    private static final Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);
    @Resource
    AdminDao adminDao;

    @Resource
    HttpServletRequest request;
    @Override
    public HashMap<String, Object> login(Admin admin, String enCode) {
        //1.获取验证码
        String code = (String) request.getServletContext().getAttribute("code");
        log.info("用户输入的验证码:{}",enCode);
        log.info("用户存储的验证码:{}",code);
        HashMap<String,Object> map=new HashMap<>();
        //2.判断验证码是否一致
        if(code.equals(enCode)){
            //3.判断用户是否存在
            Admin admins = adminDao.queryByUsername(admin.getUsername());
            if(admins!=null){
                //4.判断用户状态是否正常
                if(admins.getStatus().equals("1")){
                    //5.判断密码是否正确
                    if(admins.getPassword().equals(admin.getPassword())){

                        //存储用户标记
                        request.getServletContext().setAttribute("admin",admins);

                        map.put("message",admins);
                        map.put("status",200);
                    }else{
                        map.put("message","密码错误");
                        map.put("status",400);
                    }
                }else {
                    map.put("message","该用户已被冻结请联系管理员");
                    map.put("status",400);
                }

            }else{
                map.put("message","该用户不存在");
                map.put("status",400);
            }
        }else {
            map.put("message","验证码不正确");
            map.put("status",400);
        }

        return map;
    }

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<>();
        //设置当前页
        map.put("page",page);

        //设置查询条件对象
        UserExample example = new UserExample();
        //查询总条数
        int count = adminDao.selectCountByExample(example);
        //设置总条数
        map.put("total",count);

        //创建分页对象  参数  起始条数  数据数
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        //根据分页查询数据                                  //根据条件查询数量
        List<Admin> admins = adminDao.selectByExampleAndRowBounds(example, rowBounds);
        //设置数据
        map.put("rows",admins);

        return map;
    }


    @Override
    public HashMap<String, Object> update(Admin admin) {
        HashMap<String,Object> map = new HashMap<>();
        try {
            adminDao.updateByPrimaryKeySelective(admin);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> delete(Admin admin) {

        HashMap<String, Object> map = new HashMap<>();
        //删除数据
        try {
            adminDao.delete(admin);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }

        return map;
    }
}
